Fix cargo test filtering for binaries
authorAlex Crichton <alex@alexcrichton.com>
Sat, 21 Mar 2015 00:47:34 +0000 (17:47 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Sat, 21 Mar 2015 00:48:17 +0000 (17:48 -0700)
This adds a new `--bin` flag to `cargo test` to specifically say that a binary
should be tested. Additionally the dependencies are tweaked such that binaries
to not depend on themselves being available.

src/bin/test.rs
src/cargo/ops/cargo_compile.rs
src/cargo/ops/cargo_rustc/context.rs
tests/test_cargo_test.rs

index 6c385c277808e80d6c42b71567bd587029e9bfff..b6f9a12e99750c003a537dbe958a0ac71cf11344 100644 (file)
@@ -9,6 +9,7 @@ struct Options {
     flag_jobs: Option<u32>,
     flag_manifest_path: Option<String>,
     flag_test: Option<String>,
+    flag_bin: Option<String>,
     flag_no_default_features: bool,
     flag_no_run: bool,
     flag_package: Option<String>,
@@ -24,7 +25,8 @@ Usage:
 
 Options:
     -h, --help               Print this message
-    --test NAME              Name of the test executable to run
+    --test NAME              Name of the integration test to run
+    --bin NAME               Name of the binary to run tests for
     --no-run                 Compile, but don't run tests
     -p SPEC, --package SPEC  Package to run tests for
     -j N, --jobs N           The number of jobs to run in parallel
@@ -52,10 +54,13 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
     let root = try!(find_root_manifest_for_cwd(options.flag_manifest_path));
     config.shell().set_verbose(options.flag_verbose);
 
-    let mut tests = Vec::new();
+    let (mut tests, mut bins) = (Vec::new(), Vec::new());
     if let Some(s) = options.flag_test {
         tests.push(s);
     }
+    if let Some(s) = options.flag_bin {
+        bins.push(s);
+    }
 
     let ops = ops::TestOptions {
         no_run: options.flag_no_run,
@@ -69,12 +74,12 @@ pub fn execute(options: Options, config: &Config) -> CliResult<Option<()>> {
             exec_engine: None,
             release: false,
             mode: ops::CompileMode::Test,
-            filter: if tests.len() == 0 {
+            filter: if tests.len() == 0 && bins.len() == 0 {
                 ops::CompileFilter::Everything
             } else {
                 ops::CompileFilter::Only {
-                    lib: false, bins: &[], examples: &[], benches: &[],
-                    tests: &tests,
+                    lib: false, examples: &[], benches: &[],
+                    tests: &tests, bins: &bins,
                 }
             }
         },
index d95ae0c12ff3774b0d0d4e33bed34c98f1e74181..d74fa59b87975e0291becdbaffbec3d70e57fff5 100644 (file)
@@ -208,8 +208,9 @@ fn generate_targets<'a>(pkg: &'a Package,
                         -> CargoResult<Vec<(&'a Target, &'a Profile)>> {
     let profiles = pkg.manifest().profiles();
     let build = if release {&profiles.release} else {&profiles.dev};
+    let test = if release {&profiles.bench} else {&profiles.test};
     let profile = match mode {
-        CompileMode::Test => if release {&profiles.bench} else {&profiles.test},
+        CompileMode::Test => test,
         CompileMode::Bench => &profiles.bench,
         CompileMode::Build => build,
     };
@@ -267,14 +268,14 @@ fn generate_targets<'a>(pkg: &'a Package,
                                                               named `{}`",
                                                              desc, name))),
                         };
+                        debug!("found {} `{}`", desc, name);
                         targets.push((t, profile));
                     }
                     Ok(())
                 };
                 try!(find(bins, "bin", TargetKind::Bin, profile));
-                try!(find(examples, "example", TargetKind::Example,
-                          &profiles.dev));
-                try!(find(tests, "test", TargetKind::Test, &profiles.test));
+                try!(find(examples, "example", TargetKind::Example, build));
+                try!(find(tests, "test", TargetKind::Test, test));
                 try!(find(benches, "bench", TargetKind::Bench, &profiles.bench));
             }
             Ok(targets)
index a30b814a0beaccb78cf2c19e096fa988e13bd53a..36088778786edb6ab7ed558279ea71ffee19bc5b 100644 (file)
@@ -378,7 +378,7 @@ impl<'a, 'b: 'a> Context<'a, 'b> {
 
         // If this is a test profile, then we need to ensure that all binaries
         // are built.
-        if profile.test {
+        if profile.test && (target.is_test() || target.is_bench()) {
             ret.extend(pkg.targets().iter().filter(|t| t.is_bin())
                           .map(|t| (pkg, t, self.lib_profile(pkg.package_id()))));
         }
index 4dd56d6bba42d472ab03c26466096a7954eeb12c..8eccc5742860a350b4d7de386f09b13abed0ad1d 100644 (file)
@@ -898,7 +898,7 @@ test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
        running = RUNNING,
        dir = prj.url());
 
-    assert_that(prj.cargo_process("test").arg("--test").arg("bin2"),
+    assert_that(prj.cargo_process("test").arg("--bin").arg("bin2"),
         execs().with_status(0).with_stdout(expected_stdout.as_slice()));
 });